package com.tedu.springmvc2.controller;

import com.tedu.springmvc2.mapper.UserMapper;
import com.tedu.springmvc2.pojo.dto.UserDTO;
import com.tedu.springmvc2.pojo.entity.User;
import com.tedu.springmvc2.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @RequestMapping("/reg")
    @ResponseBody
    public String reg(UserDTO userDTO){
        System.out.println("开始处理注册需求:"+userDTO);
        User user = new User();
        BeanUtils.copyProperties(userDTO,user);
        user.setCreated(new java.util.Date());
        int re = userMapper.insert(user);
        return re > 0 ? "注册成功":"注册失败";
    }
    @RequestMapping("/update")
    @ResponseBody
    public String update(UserDTO userDTO){
        System.out.println("开始处理修改需求:"+userDTO);
        User user = new User();
        BeanUtils.copyProperties(userDTO,user);
        userMapper.update(user);
        return userMapper.update(user)>0?"修改成功":"修改失败";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String delete(Integer id){
        System.out.println("用户删除工作");
        return  userMapper.delete(id)>0?"删除成功":"删除失败";
    }

    @RequestMapping("/findAll")
    @ResponseBody
    public List<UserVO> findAll(){
        return userMapper.findAll();
    }


}
